package com.party.business.controller;


import com.party.business.domain.TComment;
import com.party.business.service.ITCommentService;
import com.party.common.annotation.Log;
import com.party.common.core.controller.BaseController;
import com.party.common.core.domain.AjaxResult;
import com.party.common.core.domain.entity.SysUser;
import com.party.common.core.page.TableDataInfo;
import com.party.common.enums.BusinessType;
import com.party.common.exception.ServiceException;
import com.party.common.utils.SecurityUtils;
import com.party.common.utils.StringUtils;
import com.party.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 评论模块
 */
@RestController
@RequestMapping("/business/apiComment")
public class ApiCommentController extends BaseController {
    @Autowired
    private ITCommentService tCommentService;



    @Autowired
    private ISysUserService sysUserService;


    /**
     * 查询评论列表分页
     */
    @GetMapping("/nologin/list")
    public TableDataInfo list(TComment tComment) {
        startPage();
        // 第一级别
        tComment.setParentCommentId("0");
        List<TComment> list = tCommentService.selectTCommentList(tComment);
        for (int i = 0; i < list.size(); i++) {
            TComment comment = list.get(i);
            // 查询第二级所有的评论
            // PageUtils.startPage(1, 3);
            TComment childcoment = new TComment();
            childcoment.setParentCommentId(comment.getId());
            List<TComment> childtComments = tCommentService.selectTCommentList(childcoment);
            list.get(i).setChildList(childtComments);
        }
        return getDataTable(list);
    }

    /**
     * 获得二级评论
     */
    @GetMapping("/nologin/getChildComment")
    public AjaxResult getChildComment(TComment vo) {
        startPage();
        if (StringUtils.isEmpty(vo.getParentCommentId())) {
            throw new ServiceException("获得参数失败");
        }
        List<TComment> childtComments = tCommentService.selectTCommentList(vo);
        return AjaxResult.success(childtComments);
    }

    /**
     * 获取评论详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        TComment comment = tCommentService.selectTCommentById(id);
        if (comment == null) {
            throw new SecurityException("评论信息不见了");
        }
        return AjaxResult.success(comment);
    }

    /**
     * 新增评论
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TComment tComment) {
        // 敏感词 过滤
        tComment.setUserId(SecurityUtils.getUserIdStr());
        tCommentService.save(tComment);
        SysUser sysUser = sysUserService.selectUserById(SecurityUtils.getUserId());
        if(sysUser!=null){
            tComment.setNickName(sysUser.getNickName());
            tComment.setAvatar(sysUser.getAvatar());
        }
        return AjaxResult.success(tComment);
    }

    /**
     * 删除评论
     */
    @Log(title = "评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tCommentService.removeByIds(Arrays.asList(ids)));
    }
}
